package repositoryimpl

import (
	"strings"

	"cvevulner/taskhandler"
)

type CveInfo struct {
	CveNum          string  `json:"cve_num"`
	IssueNum        string  `json:"issue_num"`
	Repo            string  `json:"repo"`
	OwnedComponent  string  `json:"owned_component"`
	CveBrief        string  `json:"cve_brief"`
	CveLevel        string  `json:"cve_level"`
	AffectedVersion string  `json:"affected_version"`
	Introduction    string  `json:"introduction"`
	Summary         string  `json:"summary"`
	Description     string  `json:"description"`
	AffectProduct   string  `json:"affect_product"`
	WillFixProduct  string  `json:"will_fix_product"`
	ReferenceLink   string  `json:"reference_link"`
	OpeneulerScore  float64 `json:"openeuler_score"`
	Theme           string  `json:"theme"`
	OpeneulerVector string  `json:"openeuler_vector"`
	CveVersion      string  `json:"cve_version"`
	AbiVersion      string  `json:"abi_version"`
}

// 是否是新增了分析说明模块的数据
func (c CveInfo) IsIssueWithAnalysis() bool {
	return c.WillFixProduct != ""
}

func (c CveInfo) GetAffectProduct() string {
	if c.IsIssueWithAnalysis() {
		return c.WillFixProduct
	} else {
		return c.AffectProduct
	}
}

// GetComponentDesc 获取cve正确的描述内容
func (c CveInfo) GetComponentDesc() string {
	const splitLen = 2
	subDescription := strings.ReplaceAll(c.Description, "\n\n", "\r\n\r\n")
	subDescription = taskhandler.XmlSpecCharHand(subDescription)
	dSplit := strings.Split(subDescription, "Security Fix(es):")
	if len(dSplit) >= splitLen {
		return dSplit[0]
	}

	return ""
}
